package com.concurrent.base;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class MyCallable implements Callable<Integer> {
    /**
     * 类似与runnable，但是有返回值, 返回值类型通过范型指定
     * @return
     * @throws Exception
     */
    @Override
    public Integer call() throws Exception {
        System.out.println("this is callableThead");
        return 123;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable myCallable = new MyCallable();
        //Callable 可以有返回值，返回值通过 FutureTask 进行封装
        FutureTask futureTask = new FutureTask(myCallable);
        Thread thread = new Thread(futureTask);
        thread.start();
        //通过FutureTask对象的get方法，可以获得子线程的返回值
        System.out.println(futureTask.get());
    }
}
